*** Gender and Family Ties in Latin American Legislatures
*** Replication File
*** June 5, 2020
*** Part 1

set more off

** Set the working directory**
capture cd "~/Gender and Legacies/Replication"   

**Open the dataset
use newdata.dta, clear

**** Paper Section: An Empirical Analysis of Gender and Family Ties in Latin America

* In-text Statistics: Dataset Information
* Total Number of observations in Dataset
sum surveywave
* Count number of country-waves
tab nestu 

*Dropping out Paraguay
drop if nestu==21
drop if nestu==49

*Dropping out the second survey for Peru in Wave 3
drop if nestu==80

*(See footnote 6 on the exclusion of those cases from our data base )

* In-text Statistics:  Average % of survey respondents with a family tie 
tab legacy

* In-text Statistics:  Comparing average % legislators with family ties across countries 
set more off
bysort couname: tab legacy

*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
******** Figure 1: Family Ties by Country and Survey Wave ***********
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************

* Create variable for average percentage of legislators with legacy ties by country-survey wave
* Note: the denominator of this variable is the total number of legislators in every survey wave, including those with missing values for legacy
sort nestu
by nestu: egen m_legacy=mean(legacy) 
gen p_legacy=m_legacy*100


** Set colors 
global color1 "248 118 109"
global color2 "124 174 0"
global color3 "0 184 31"
global color4 "0 191 196"
global color5 "199 124 255"



* Create Figure 1
graph bar (mean) p_legacy, over(surveywave, axis(off) lab(nolab)) by(couname, legend(pos(6)) bgcolor(white)  note(" ") ///
graphregion(fcolor(white) lcolor(white))) asyvars ///
subtitle(,fcolor(gs13) lcolor(white))  ///
ylabel(,grid glcol(white) tlc(black))  ///
ytitle("Percentage of legislators with family ties") ///
	bar(1, color(navy*.9) lw(none)) bar(2, color(red*.7) lw(none)) bar(3, color(green*.7) lw(none)) /// Some lines to make the graphs look fancy 
	bar(4, color(purple*.7) lw(none)) bar(5, color(gold*.7) lw(none)) /// 
	legend(row(1) size(small) region(col(white))) ///
	plotregion(color(white)) yscale(lc(black)) scheme(s1color) ///
	subtitle(, box ///
	bexpand fcolor(gs13) lwidth(none) size()) ///
	ylabel(,grid glc(gs1) glp(dot))
*graph save "bars_by_countryandwave.gph", replace
*graph export "bars_by_countryandwave.png", replace width(6000)
	graph export "figure_1_color.tif", replace width(1000)
	
* Figure 1 Greyscale
graph bar (mean) p_legacy, over(surveywave, axis(off) lab(nolab)) by(couname, legend(pos(6)) bgcolor(white)  note(" ") ///
graphregion(fcolor(white) lcolor(white))) asyvars ///
subtitle(,fcolor(gs13) lcolor(white))  ///
ylabel(,grid glcol(white) tlc(black))  ///
ytitle("Percentage of legislators with family ties") ///
	bar(1, color(black*2) lw(none)) bar(2, color(gs11*.7) lw(none)) bar(3, color(gs3*.7) lw(none)) /// Some lines to make the graphs look fancy 
	bar(4, color(gs10*.9) lw(none)) bar(5, color(gs9*.9) lw(none)) /// 
	legend(row(1) size(small) region(col(white))) ///
	plotregion(color(white)) yscale(lc(black)) scheme() ///
	subtitle(, box ///
	bexpand fcolor(gs13) lwidth(none) size()) ///
	ylabel(,grid glc(gs1) glp(dot))
	graph export "figure_1_bw.tif", replace width(1000)

* Alternative Figure 1 with years in legend

/* graph bar (mean) p_legacy, over(surveywave) by(couname, note(" ")) graphregion(fcolor(white) lcolor(white)) asyvars ///
subtitle(,fcolor(white) lcolor(white))  ///
ylabel(,grid glcol(gs9)) ///
ytitle("Percentage of legislators with family ties") ///
legend(order(1 "Wave 2 (1998-2002)" 2 "Wave 3 (2000-2006)" 3 "Wave 4 (2003-2010)" ///
4 "Wave 5 (2006, 2009-2011)" 5 "Wave 6 (2009-2011)") row(2) pos(6) ring(2) size(small))
graph save "bars_by_countryandwave.gph", replace
graph export "bars_by_countryandwave.png", replace */


* In-Text Statistics:  Percentage of legislators with family ties by country and survey wave
bysort couname: tab legacy surveywave, col missing


*** Paper Section: A Bivariate Analysis of Sex and Family Ties 

* In-text Statistics:  Percentage of male and female legislators with family ties (pooled across countries)
* Note: It excludes individuals with missing values for gender or legacy
tab gender legacy, row chi2 

* In-text Statistics:  Percentage of male and female legislators wtih family ties by country (pooled across survey waves)
* Note: It excludes individuals with missing values for gender or legacy
set more off
bysort couname: tab gender legacy, row chi2 

*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
*********** Figure 2: Family Ties by Country and Sex ****************
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************

* Create variable for average percentage of legacy ties by country and gender
* Note: the denominator of this variable is the total number of legislators in every survey wave, including those with missing values for legacy
sort ccode gender
g legacy100=legacy*100 // Transforming shares into percentages

label define cou_lbl 1 "ARGENTINA" 2 "BOLIVIA" 3 "BRAZIL" 4 "CHILE" 5 "COLOMBIA" ///
6 "COSTA RICA" 16 "DOMINICAN REPUBLIC" 7 "ECUADOR" 8 "EL SALVADOR" 9 "GUATEMALA" 10 "HONDURAS" ///
11 "MEXICO" 12 "NICARAGUA" 13 "PANAMA" 15 "PERU" 17 "URUGUAY" 18 "VENEZUELA"
label values ccode cou_lbl


forvalues i = 1/18 {
capture tab gender legacy if ccode==`i', row chi2 // Run a Chi2 for each country
capture local p = string(`r(p)', "%3.2f") // Store the p value in a local variable
capture local t : label cou_lbl `i' // Store the lavbl of the country
capture graph bar (mean) legacy100 if ccode==`i', over(gender, axis(lw(none))) yscale(r(0 50)) /// Create the graph bar
ytitle("") scheme(s1color)  /// Design and colors of the graph
	plotregion(color(white)) yscale(lc(black))  /// 
	bar(1, color(navy*.9) lcolor(none)) title(, box /// 
	bexpand fcolor(gs13) lwidth(none) size(medium)) /// 
	ylabel(0(10)50,grid glcolor(gs1) glp(dot) tlc(black) ) /// 
	subtitle("P-value = `p'", size() box bexpand lwidth(none) fcolor(gs14)) /// Include the stored p-value in the subtitle
	title(`t', pos(12)) name(g`i'_with_p, replace) nodraw // Add the Country name stored in the local as the title of each bar graph
}
graph combine g1_with_p g2_with_p g3_with_p g4_with_p g5_with_p g6_with_p g16_with_p g7_with_p g8_with_p g9_with_p /// Combine all graphs by country
g10_with_p g11_with_p g12_with_p g13_with_p g15_with_p g17_with_p g18_with_p, l1(Percetage of legislators with family ties, size(small)) graphregion(fcolor(white)) ycommon
*graph save "bars_by_country.gph", replace
*graph export "bars_by_country.png", replace width(6000)
graph export "figure_2_color.tif", replace width(1000)

*Figure 2 Greyscale
forvalues i = 1/18 {
capture tab gender legacy if ccode==`i', row chi2 // Run a Chi2 for each country
capture local p = string(`r(p)', "%3.2f") // Store the p value in a local variable
capture local t : label cou_lbl `i' // Store the lavbl of the country
capture graph bar (mean) legacy100 if ccode==`i', over(gender, axis(lw(none))) yscale(r(0 50)) /// Create the graph bar
ytitle("") scheme()  /// Design and colors of the graph
	plotregion(color(white)) yscale(lc(black))  /// 
	bar(1, color(black*1) lcolor(none)) title(, box /// 
	bexpand fcolor(gs13) lwidth(none) size(medium)) /// 
	ylabel(0(10)50,grid glcolor(gs1) glp(dot) tlc(black) ) /// 
	subtitle("P-value = `p'", size() box bexpand lwidth(none) fcolor(gs14)) /// Include the stored p-value in the subtitle
	title(`t', pos(12)) name(g`i'_with_p, replace) nodraw // Add the Country name stored in the local as the title of each bar graph
}
graph combine g1_with_p g2_with_p g3_with_p g4_with_p g5_with_p g6_with_p g16_with_p g7_with_p g8_with_p g9_with_p /// Combine all graphs by country
g10_with_p g11_with_p g12_with_p g13_with_p g15_with_p g17_with_p g18_with_p, l1(Percetage of legislators with family ties, size(small)) graphregion(fcolor(white)) ycommon
	graph export "figure_2_bw.tif", replace width(1000)


*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
******** Figure 3: Family Ties by Sex and Survey Wave ************
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************

* In-text Statistics:  Percentage of male and female legislators with family ties by survey waves
bysort surveywave: tab legacy

label define wave_lbl 2 "Wave 2" 3 "Wave 3" 4 "Wave 4" 5 "Wave 5" 6 "Wave 6"

forvalues i = 2/6 {
 tab gender legacy if surveywave==`i', row chi2 // Run a Chi2 for each survey wave
 local p = string(`r(p)', "%3.2f") // Store the p value in a local variable
 local t : label wave_lbl `i' // Store the lavbl of the wave
 graph bar (mean) legacy100 if surveywave==`i', over(gender, axis(off)) yscale(r(0 40)) /// Create the graph
ylabel(0(10)40, grid glcol(white) tlc(black)) ytitle("") scheme(s1color)  ///
	legend(row(1) size(small) region(col(white))) ///
	plotregion(color(white)) yscale(lc(black))  ///
	bar(1, color(navy*.9) lcolor(none)) title(, box ///
	bexpand fcolor(gs13) lwidth(none) size(medium)) ///
	ylabel(,grid glcolor(gs1) glp(dot)) ///
subtitle("P-value = `p'", size() box bexpand lwidth(none) fcolor(gs14)) title(`t', pos(12)) name(g`i'_with_pw, replace) nodraw // Insert P value as subtitle
}
graph combine g2_with_pw g3_with_pw g4_with_pw g5_with_pw g6_with_pw, l1(Percetage of legislators with family ties, size(small)) ycommon graphregion(fcolor(white)) // Combine the graphs by wave
*graph save "bars_by_wave.gph", replace
*graph export "bars_by_wave.png", replace width(500)
	graph export "figure_3_color.tif", replace width(1000)


* Figure 3
forvalues i = 2/6 {
 tab gender legacy if surveywave==`i', row chi2 // Run a Chi2 for each survey wave
 local p = string(`r(p)', "%3.2f") // Store the p value in a local variable
 local t : label wave_lbl `i' // Store the lavbl of the wave
 graph bar (mean) legacy100 if surveywave==`i', over(gender, axis(off)) yscale(r(0 40)) /// Create the graph
ylabel(0(10)40, grid glcol(white) tlc(black)) ytitle("") scheme()  ///
	legend(row(1) size(small) region(col(white))) ///
	plotregion(color(white)) yscale(lc(black))  ///
	bar(1, color(black*1) lcolor(none)) title(, box ///
	bexpand fcolor(gs13) lwidth(none) size(medium)) ///
	ylabel(,grid glcolor(gs1) glp(dot)) ///
subtitle("P-value = `p'", size() box bexpand lwidth(none) fcolor(gs14)) title(`t', pos(12)) name(g`i'_with_pw, replace) nodraw // Insert P value as subtitle
}
graph combine g2_with_pw g3_with_pw g4_with_pw g5_with_pw g6_with_pw, l1(Percetage of legislators with family ties, size(small)) ycommon graphregion(fcolor(white)) // Combine the graphs by wave
	graph export "figure_3_bw.tif", replace width(1000)
	
	

*** Paper Section:  A Multivariate Analysis of Sex and Family Ties

*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
******** Table 1: Effect of gender of legacies **********************
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************


***************************************************************************************
* Model 1: Multivariate Analysis of Sex and Family Ties in Latin American Legislatures*
***************************************************************************************

eststo clear
eststo m1 : logit legacy gender age education ideology_legis previous_exp junior i.ccode i.surveywave


*** Paper Sub-Section:  The Moderating Effect of Quotas

* In-text Statistics:  Percentage of legislators elected in a system with quotas
tab quota
bysort surveywave: tab quota

* In-text Statistics:  Bivariate analysis of difference in means (results reported in text only and fn 11; no table/figure)
tab gender legacy if quota==1, row chi2
tab gender legacy if quota==0, row chi2


*********************************************************************
* Model 2: Analysis only including the interaction between gender *** 
*          and legislative quota ************************************
*********************************************************************

eststo m2: logit legacy gender##quota i.ccode i.surveywave

*********************************************************************
* Model 3: Multivariate model including all our control variables *** 
*********************************************************************

eststo m3: logit legacy  gender##quota age education previous_exp junior ideology_legis i.ccode i.surveywave


**Putting the three models together in TABLE 1**

esttab m1 m2 m3 using table_1.rtf, mtitles("Model 1" "Model 2" "Model 3") b(3) star se nonotes noomit nocons nogaps label ///
 drop(*.ccode *.surveywave 0.gender 0.quota 0.gender#0.quota) interaction(" * ") noomitted starlevels(* 0.10 ** 0.05 *** 0.01)   scalars(N chi2 ll) stats(chi2 N ll , star(chi2)) replace
eststo clear


*************************************************************************************
*************************************************************************************
*************************************************************************************
*************************************************************************************
******** Figures 4 and 5: Predicted probabilities of the effect of quotas ***********
*************************************************************************************
*************************************************************************************
*************************************************************************************
*************************************************************************************

**Figure 4 **

* Re-run Model 1 of Table 1
set more off
logit legacy gender age education ideology_legis previous_exp junior i.ccode i.surveywave

* Estimate and graph the mean difference in predicted probabilities for men and women and 95% confidence intervals, holding other attributes at their sample means
ssc install more_clarify, replace
set seed 12345
postsim, saving(sims_f4, replace) reps(1000): logit legacy gender age education ideology_legis previous_exp junior i.ccode i.surveywave

simqoi using sims_f4,  gen(pr_lega_f4_0) at(gender=(0))  // Generate Predictions
simqoi using sims_f4,  gen(pr_lega_f4_1) at(gender=(1))  // Generate Predictions

preserve

g mean=. // Store the predictions
g upper=.
g lower=.
g xaxis=. if _n==1
replace xaxis=0 if _n==1
replace xaxis=1 if _n==2

sum pr_lega_f4_0 // Caulculate confidence intervals using standar deviation
replace mean=r(mean) if _n==1
replace upper=1.96*r(sd)+r(mean) if _n==1
replace lower=r(mean)-1.96*r(sd) if _n==1

sum pr_lega_f4_1
replace mean=r(mean) if _n==2
replace upper=1.96*r(sd)+r(mean) if _n==2
replace lower=-1.96*r(sd)+r(mean) if _n==2

twoway (scatter mean xaxis, m(o) mc(navy*.9)) (rspike upper lower xaxis, lc(navy*.9)), ///
ytitle(Predicted probabilities) xtitle("")  scheme() ///
xlabel(0 "Men" 1 "Women", nogrid noticks) xsc(r(-.3 1.3) noline ) legend(off) ylabel(.275(.025).375) ysc(r(.275 .375)) ///
plotregion(color(white)) yscale(lc(black)) ylabel(,grid glcolor(gs1) glp(dot) tlc(black)) 
*graph save "predprob_f4.gph", replace
*graph export "predprob_f4.png", replace width(6000)
graph export "figure_4_color.tif", replace width(1000)

twoway (scatter mean xaxis, m(o) mc(gs3*.7)) (rspike upper lower xaxis, lc(gs3*.7)), ///
ytitle(Predicted probabilities) xtitle("")  scheme() ///
xlabel(0 "Men" 1 "Women", nogrid noticks) xsc(r(-.3 1.3) noline ) legend(off) ylabel(.275(.025).375) ysc(r(.275 .375)) ///
plotregion(color(white)) yscale(lc(black)) ylabel(,grid glcolor(gs1) glp(dot) tlc(black)) 
	graph export "figure_4_bw.tif", replace width(1000)

restore


** Figure 5 **

* Re-run Model 3 of Table 1
set more off
logit legacy  gender##quota age education previous_exp junior ideology_legis i.ccode i.surveywave

* Estimate and graph the mean difference in predicted probabilities for men and women and 95% confidence intervals, holding other attributes at their sample means
ssc install more_clarify, replace
set seed 12345
postsim, saving(sims_f6, replace) reps(1000): logit legacy  gender##quota age education previous_exp junior ideology_legis i.ccode i.surveywave

simqoi using sims_f6, diff  gen(pr_quota0) at(gender=(0 1) quota=(0))  // Generate Predictions
simqoi using sims_f6, diff  gen(pr_quota1) at(gender=(0 1) quota=(1)) 

preserve

g mean=. // Store the predictions
g upper=.
g lower=.
g xaxis=. if _n==1
replace xaxis=0 if _n==1
replace xaxis=1 if _n==2

sum pr_quota0_1 // Caulculate confidence intervals using standar deviation
replace mean=r(mean) if _n==1
replace upper=1.96*r(sd)+r(mean) if _n==1
replace lower=r(mean)-1.96*r(sd) if _n==1

sum pr_quota1_1
replace mean=r(mean) if _n==2
replace upper=1.96*r(sd)+r(mean) if _n==2
replace lower=-1.96*r(sd)+r(mean) if _n==2

twoway (scatter mean xaxis, m(o) mc(navy*.9)) (rspike upper lower xaxis, lc(navy*.9)), ///
yline(0, lcol(red) lp(solid)) ytitle(Predicted probabilities) xtitle("")  scheme() ///
xlabel(0 "No Quota" 1 "Quota", nogrid notick) xsc(r(-.3 1.3) noline) legend(off)  ///
	plotregion(color(white)) yscale(lc(black)) ///
	ylabel(,grid glcolor(gs1) glp(dot) tlc(black)) 
*graph save "difference_in_predprob_f5.gph", replace
*graph export "difference_in_predprob_f5.png", replace width(6000)
graph export "figure_5_color.tif", replace width(1000)

twoway (scatter mean xaxis, m(o) mc(gs3*.7)) (rspike upper lower xaxis, lc(gs3*.7)), ///
yline(0, lcol(black) lp(solid)) ytitle(Predicted probabilities) xtitle("")  scheme() ///
xlabel(0 "No Quota" 1 "Quota", nogrid notick) xsc(r(-.3 1.3) noline) legend(off)  ///
	plotregion(color(white)) yscale(lc(black)) ///
	ylabel(,grid glcolor(gs1) glp(dot) tlc(black)) 
graph export "figure_5_bw.tif", replace width(1000)


restore 

*** Calculate the raw predicted probabilities for the text
simqoi using sims_f6, gen(pr_quota0_each) at(gender=(0 1) quota=(0))  // Generate Predictions
simqoi using sims_f6, gen(pr_quota1_each) at(gender=(0 1) quota=(1)) 


**************************************************************
**** Figure 6: Legacies over time in Mexico and Honduras ****
**************************************************************

preserve

* Create variable for average percentage of legislators with legacy ties by country-survey wave and gender
sort ccode gender

* Collapse the database by gender and wave
collapse (mean) legacy100 (first) couname sessionbeg, by(nestu gender)

* Keep relevant cases
keep if couname == "MEXICO" | couname =="HONDURAS"

* Drop missing cases
drop if gender==.

* Reshape database so legacy rates by year have two columns, one for each gender
reshape wide legacy100, i(couname sessionbeg) j(gender)

rename legacy1000 legacy_men
rename legacy1001 legacy_women

*Labeling variables
label variable legacy_men "Men legislators"
label variable legacy_women "Women legislators"
label variable sessionbeg "Year"

***Bar Graphs***

*Generate x-axis"
g xa = sessionbeg-.4
g xb = sessionbeg+.4
*Generate percentage as a string variable to include it in the graph
g label_men=string(int(legacy_men))
g label_women=string(int(legacy_women))

*Graph for Honduras*
twoway  (bar legacy_men xa if couname=="HONDURAS", barwidth(.8) col(navy*.9) lw(none)) /// Bar Men
		(bar legacy_women xb if couname=="HONDURAS", barwidth(.8) col(cranberry*.9) lw(none)) /// Bar Women
		(scatter legacy_men xa if couname=="HONDURAS", mlabel(label_men) mlabpos(12) ms(none) mlabcolor(black) mlabs(small)) /// Value for men
		(scatter legacy_women xb if couname=="HONDURAS", mlabel(label_women) mlabpos(12) ms(none) mlabcolor(black) mlabs(small)), /// Value for women
graphregion(fcolor(white) lcolor(white)) /// Formatting
title(Honduras, size() box bexpand lwidth(none) fcolor(gs13)) ///
subtitle(,fcolor(white) lcolor(white)) sch(s1color) ///
ylabel(,grid glcol(gs1) glp(dot) tlc(black)) plotregion(color(white)) ///
xline(2000, lp(dash) lc(red) lw(medium)) ///
xlabel(1997 2002(4)2010 2000, labs(small) nogrid tlc(black)) ylabel(0(10)70, labs(small)) xsc(lc(black)) ysc(lc(black)) ///
ytitle("") ///
legend(order(1 2) col(2) pos(6) size(small)) name(honduras, replace) title(Honduras)

*Graph for Mexico*
twoway  (bar legacy_men xa if couname=="MEXICO", barwidth(.8)  col(navy*.9) lw(none)) /// Bar Men
		(bar legacy_women xb if couname=="MEXICO", barwidth(.8) col(cranberry*.9) lw(none)) /// Bar Women
		(scatter legacy_men xa if couname=="MEXICO", mlabel(label_men) mlabpos(12) ms(none) mlabcolor(black) mlabs(small)) /// Value for men
		(scatter legacy_women xb if couname=="MEXICO", mlabel(label_women) mlabpos(12) ms(none) mlabcolor(black) mlabs(small)), /// Value for women
graphregion(fcolor(white) lcolor(white)) /// Formatting
title(Mexico, size() box bexpand lwidth(none) fcolor(gs13)) ///
subtitle(,fcolor(white) lcolor(white)) sch(s1color) ///
ylabel(,grid glcol(gs1) glp(dot) tlc(black)) plotregion(color(white)) ///
xline(2002, lp(dash) lc(red) lw(medium)) ///
xlabel(1997(3)2009 2002, labs(small) nogrid tlc(black)) ylabel(0(10)70, labs(small)) xsc(lc(black)) ysc(lc(black)) ///
ytitle("") ///
legend(order(1 2) col(2) pos(6)) name(mexico, replace) title(Mexico)

*Combine the two graphs*
grc1leg honduras mexico, l1(Percentage of legislators with family ties, size(small))
graph display, xsize(7) // Resize the Graph
*graph save "hondurasmexico.gph", replace
*graph export "hondurasmexico.png", replace width(6000) // Export the grap in high definition
	graph export "figure_6_color.tif", replace width(1000)

	
	


*Graph for Honduras*
twoway  (bar legacy_men xa if couname=="HONDURAS", barwidth(.8) col(gs1*.7) lw(none)) /// Bar Men
		(bar legacy_women xb if couname=="HONDURAS", barwidth(.8) col(gs10*.7) lw(none)) /// Bar Women
		(scatter legacy_men xa if couname=="HONDURAS", mlabel(label_men) mlabpos(12) ms(none) mlabcolor(black) mlabs(small)) /// Value for men
		(scatter legacy_women xb if couname=="HONDURAS", mlabel(label_women) mlabpos(12) ms(none) mlabcolor(black) mlabs(small)), /// Value for women
graphregion(fcolor(white) lcolor(white)) /// Formatting
title(Honduras, size() box bexpand lwidth(none) fcolor(gs13)) ///
subtitle(,fcolor(white) lcolor(white))  ///
ylabel(,grid glcol(gs1) glp(dot) tlc(black)) plotregion(color(white)) ///
xline(2000, lp(dash) lc(black) lw(medium)) ///
xlabel(1997 2002(4)2010 2000, labs(small) nogrid tlc(black)) ylabel(0(10)70, labs(small)) xsc(lc(black)) ysc(lc(black)) ///
ytitle("") ///
legend(order(1 2) col(2) pos(6) size(small)) name(honduras, replace) title(Honduras)

*Graph for Mexico*
twoway  (bar legacy_men xa if couname=="MEXICO", barwidth(.8)  col(gs1*.7) lw(none)) /// Bar Men
		(bar legacy_women xb if couname=="MEXICO", barwidth(.8) col(gs10*.7) lw(none)) /// Bar Women
		(scatter legacy_men xa if couname=="MEXICO", mlabel(label_men) mlabpos(12) ms(none) mlabcolor(black) mlabs(small)) /// Value for men
		(scatter legacy_women xb if couname=="MEXICO", mlabel(label_women) mlabpos(12) ms(none) mlabcolor(black) mlabs(small)), /// Value for women
graphregion(fcolor(white) lcolor(white)) /// Formatting
title(Mexico, size() box bexpand lwidth(none) fcolor(gs13)) ///
subtitle(,fcolor(white) lcolor(white))  ///
ylabel(,grid glcol(gs1) glp(dot) tlc(black)) plotregion(color(white)) ///
xline(2002, lp(dash) lc(black) lw(medium)) ///
xlabel(1997(3)2009 2002, labs(small) nogrid tlc(black)) ylabel(0(10)70, labs(small)) xsc(lc(black)) ysc(lc(black)) ///
ytitle("") ///
legend(order(1 2) col(2) pos(6)) name(mexico, replace) title(Mexico)

*Combine the two graphs*
net install grc1leg.pkg, replace
grc1leg honduras mexico, l1(Percentage of legislators with family ties, size(small)) ycommon
graph display, xsize(7) // Resize the Graph
	graph export "figure_6_bw.tif", replace width(1000)
	
	
	
restore


*********************************************************************
************************ APPENDIX ***********************************
*********************************************************************

* APPENDIX TABLE A2: DESCRIPTIVE STATISTICS 
ssc install asdoc, replace
asdoc sum legacy gender quota age education previous_exp junior ideology_legis, stat(N mean sd min max) label replace save(table_a2.doc) title(Table A.2: Descriptive Statistics) 

* APPENDIX TABLE A3: Multivariate Analysis of Sex and Family Ties in Latin American Legislatures (Including Surveys with Responses Rates for at least 70% of Legislators) 

bysort surveywave couname nestu: gen total_respondents = _N
gen response_rate = total_respondents/body_size*100
gen response=0
replace response=1 if response_rate>=70

eststo clear
eststo table_a3 : logit legacy gender age education ideology_legis previous_exp junior i.ccode i.surveywave if response_rate>=70

esttab table_a3 using table_a3.rtf, mtitles("Model 1" ) star se nonotes noomit nocons nogaps label ///
drop(*.ccode *.surveywave) interaction(" * ") noomitted starlevels(* 0.10 ** 0.05 *** 0.01)   scalars(N r2_p chi2) stats(chi2 N r2_p , star(chi2)) replace
eststo clear



* APPENDIX TABLE A4: MULTILEVEL LOGIT WITH RANDOM EFFECTS
eststo clear
eststo model1_RE: xtmelogit legacy gender age education ideology_legis previous_exp junior || _all:R.ccode || surveywave:
eststo model2_RE: xtmelogit legacy gender##quota || _all:R.ccode || surveywave:
eststo model3_RE: xtmelogit legacy gender##quota age education previous_exp junior ideology_legis || _all:R.ccode || surveywave:

esttab model1_RE model2_RE model3_RE using table_A4.rtf, mtitles("Model 1" "Model 2" "Model 3") star se nonotes noomit nocons nogaps label ///
 drop(  0.gender 0.quota 0.gender#0.quota) interaction(" * ") noomitted starlevels(* 0.10 ** 0.05 *** 0.01)   scalars(N r2_p chi2) stats(chi2 N r2_p , star(chi2)) replace ///
eqlabels("" "Country (Variance)" "Survey Wave (Variance)", none)
eststo clear

* APPENDIX TABLE A5: ANALYSIS OF RESIDUALS
*Residuals*
set more off
logit legacy gender quota gender_quota i.ccode i.surveywave
predict res1, residual

eststo: reg res1 ideology_legislator
eststo: reg res1 ideology_legislator age education previous_exp junior

esttab using table_A5.rtf, se r2 title("Analysis of residuals") mtitles("No Controls" "With Controls") replace
eststo clear

*APPENDIX TABLE A6: Multivariate model including all our control variables and the interaction between gender and legislator ideology

eststo table_a6: logit legacy gender quota gender_quota age education gender##c.ideology_legis previous_exp junior i.ccode i.surveywave

esttab table_a6 using table_A6.rtf, se r2 title("Table A.6: Multivariate model including all our control variables and the interaction between gender and legislator") ///
 mtitles("") replace noomitted starlevels(* 0.10 ** 0.05 *** 0.01)  label scalars(N r2_p chi2) stats(chi2 N r2_p , star(chi2)) ///
 drop(*.ccode *.surveywave 0.gender)
eststo clear


*APPENDIX FIGURE A2: HIGH RESPONSE FIGURE **
capture g legacy100=legacy*100 

forvalues i = 1/18 {
capture tab gender legacy if ccode==`i' & response_rate>=70, row chi2 
capture local p = string(`r(p)', "%3.2f")
capture local t : label cou_lbl `i'
capture graph bar (mean) legacy100 if ccode==`i' & response_rate>=70, over(gender, axis(lw(none))) yscale(r(0 50)) /// Create the graph bar
ytitle("") scheme(s1color)  /// Design and colors of the graph
	plotregion(color(white)) yscale(lc(black))  /// 
	bar(1, color(navy*.9) lcolor(none)) title(, box /// 
	bexpand fcolor(gs13) lwidth(none) size(medium)) /// 
	ylabel(0(10)50,grid glcolor(gs1) glp(dot) tlc(black) ) /// 
	subtitle("P-value = `p'", size() box bexpand lwidth(none) fcolor(gs14)) title(`t', pos(12)) name(g`i'_with_p70, replace) nodraw
}

 graph combine g2_with_p70 g4_with_p70 g6_with_p70 g16_with_p70 g7_with_p70 g8_with_p70 g9_with_p70 ///
g10_with_p70 g12_with_p70 g13_with_p70 g15_with_p70  g17_with_p70, l1(Percetage of legislators with family ties, size(small)) ycommon graphregion(fcolor(white))
*graph save "figure_a2.gph", replace
*graph export "figure_a2.png", replace width(6000)
graph export "figure_a2_color.tif", replace width(1000)


** APPENDIX A7: Table for Office**

g office=""   // Generate a string variable for the office of the prelegacy
replace office=fam_p081 if legacy==1 // The PELA survey ask respondent three times for office of relatives in poltiics (fam_p081, fam_p082, fam_p083).  
replace office=fam_p082 if missing(office) // If the value in the first answer is missing, we replace for their second/third answer.
replace office=fam_p083 if missing(office)


local list_offices JUDICIARY ADVISER DIPLOMAT OTHER UNION "DON'T ANSWER" "DON'T REMEMBER" /// Also, if they respond any of the offices we are not considering, we replace for the next answer
"NO POSITION"  "PARTY OFFICIAL" MILITARY "PUBLIC OFFICIAL"  // We do this until we get either one of the offices we consider as legacy, or nothing. 
foreach noff in `list_offices' { 
replace office=fam_p082 if office=="`noff'" & legacy==1
}
foreach noff in `list_offices' {
replace office=fam_p083 if office=="`noff'" & legacy==1
}
g noleg=0
foreach noff in `list_offices' {
replace noleg=1 if office=="`noff'"
}
replace noleg=1 if missing(office)

*Recode the repondents with no legacies in office as "NO PRELEGACY WITH OFFICE"
replace office="NO PRELEGACY WITH OFFICE" if office==""
replace office="NO PRELEGACY WITH OFFICE" if office=="NO POSITION" // Put the NO POSITION and NO PRELEGACY IN OFFICE together

*Export the table in six parts*
net install http://www.stata.com/users/kcrow/tab2xl, replace
tab2xl  office gender if noleg==0 using office_gender_leg, col(2) row(2)  
tab2xl  office gender if quota==0 & noleg==0 using office_gender_q0_leg, col(2) row(2)  
tab2xl  office gender if quota==1 & noleg==0 using office_gender_q1_leg, col(2) row(2)  

tab2xl  office gender  if noleg==1 using office_gender_noleg, col(2) row(2)  
tab2xl  office gender if quota==0 & noleg==1 using office_gender_q0_noleg, col(2) row(2)  
tab2xl  office gender if quota==1 & noleg==1 using office_gender_q1_noleg, col(2) row(2)  


* APPENDIX TABLE A8: Table for Relatives**

g relative="" // Generate a string variable for the type of relationship 
replace relative=fam_p051 if legacy==1


replace relative="" if office=="NO PRELEGACY WITH OFFICE" //Code the missing as NO PRELEGACY WITH OFFICE
replace relative="NO PRELEGACY WITH OFFICE" if relative=="" 

*Translate the offices' name of relatives from Spanish to Englush*
replace relative="Grandfather and Grandmother" if relative=="ABUELO/A"
replace relative="Great-grandfather" if relative=="BISABUELO"
replace relative="Spouse" if relative=="CONYUGUE"
*replace relative="Sister/bother in Law" if relative=="CU�ADO/A" 
replace relative="Sister/bother in Law" if intervieweeid=="BRA75054" // Stata cannot recognize the Spanish Character in CUÑADO. We translate the only observation we had in this cathegory using the respondent unique id
replace relative="Sister/Brother" if relative=="HERMANO/A"
replace relative="Daughter/Son" if relative=="HIJO/A"
replace relative="Mother" if relative=="MADRE"
replace relative="Father" if relative=="PADRE"
replace relative="Cousin" if relative=="PRIMO/A"
replace relative="Father/Mather in Law" if relative=="SUEGRO/A"
replace relative="Great Uncle" if relative=="TIO ABUELO"
replace relative="Uncle/Aunt" if relative=="TIO/A"
replace relative="OTHERS" if legacy==1 & (relative=="OTROS" | missing(relative)) 

*Export the table in three parts*
tab2xl  relative gender using relative_gender, col(2) row(2) percentage 
tab2xl  relative gender if quota==0 using relative_gender_q0, col(2) row(2) percentage 
tab2xl  relative gender if quota==1 using relative_gender_q1, col(2) row(2) percentage 

clear


* APPENDIX TABLE A: RESPONSE RATES AND GENDER QUOTAS 
bysort surveywave couname nestu: gen total_respondents = _N // Total of individuals in survey
bysort surveywave couname nestu: egen total_women_resp =sum(gender) //Total of women in survey
gen pct_women_resp= total_women/total_respondents*100 // Transform the number in a percentage
sort surveywave couname nestu

quietly by surveywave couname nestu:  gen dup = cond(_N==1,0,_n) //Keep just one variable by nestu
drop if dup>1 

keep couname surveywave legislature total_respondents total_women_resp pct_women_resp sessionbeg nestu pct_women body_size legacy// Drop unnecesary variables

sort couname surveywave 
rename body_size total_chamber // Change the name of the variables
rename pct_women pct_women_chamber
gen total_women_chamber= total_chamber*pct_women_chamber/100
replace total_women_chamber=round(total_women_chamber,1)
replace total_women_chamber=total_women_resp if total_women_chamber<total_women_resp // For some cases we have more legislators than reported being women than elected women in Chamber. It may be the case that between the election and the survey, some substitiotes got the seat instead of the original candidates
order couname surveywave nestu legislature sessionbeg total_respondents total_women_resp total_chamber total_women_chamber pct_women_chamber
drop sessionbeg  // Drop unnnecesary variables

**Calculate more percentages**
gen response_rate= total_respondents/total_chamber*100
gen response_rate_women = total_women_resp/total_women_chamber*100
gen response_rate_men = ((total_respondents-total_women_resp)/(total_chamber-total_women_chamber))*100
*replace pct_women_resp=round(pct_women_resp,.01)
replace pct_women_chamber=round(pct_women_chamber,.01)
replace response_rate=round(response_rate,.01)
replace response_rate_women=round(response_rate_women,.01)
replace response_rate_men=round(response_rate_men,.01)
replace pct_women_resp=round(pct_women_resp, 0.1)
tostring pct_women_chamber pct_women_resp response_rate response_rate_women response_rate_men, replace force
replace pct_women_chamber=substr(pct_women_chamber, 1,5)
replace pct_women_resp=substr(pct_women_resp, 1,5)
replace response_rate=substr(response_rate, 1,5)
replace response_rate_women=substr(response_rate_women, 1,5)
replace response_rate_men=substr(response_rate_men, 1,5)
destring pct_women_chamber pct_women_resp response_rate response_rate_women response_rate_men, replace force

export excel using "table_A1.xls", replace firstrow(variables) 
restore


* APPENDIX FIGURE A1: Matching Results
*open the dataset after prunning the results using optimal matching (see the "Optimal Matching.R")
use matcheddata.dta, clear 

* Labeling gender (it lost the label when transformed to .csv)
label define genderl 0 "male" 1 "female" 
label values gender genderl 

* Describe in text the overall difference between men's and women's family ties
tab gender legacy, row chi2

sort nestu
by nestu: egen m_legacy=mean(legacy)
gen p_legacy=m_legacy*100


* Create variable for average percentage of legacy ties by country and gender
* Note: the denominator of this variable is the total number of legislators in every survey wave, including those with missing values for legacy
sort couname gender
egen gd_legacy=mean(legacy) if gender!=., by (ccode gender)
gen g_legacy=gd_legacy*100


label define cou_lbl 1 "ARGENTINA" 2 "BOLIVIA" 3 "BRAZIL" 4 "CHILE" 5 "COLOMBIA" ///
6 "COSTA RICA" 16 "DOMINICAN REPUBLIC" 7 "ECUADOR" 8 "EL SALVADOR" 9 "GUATEMALA" 10 "HONDURAS" ///
11 "MEXICO" 12 "NICARAGUA" 13 "PANAMA" 15 "PERU" 17 "URUGUAY" 18 "VENEZUELA"
label values ccode cou_lbl

label define gender_lbl 0 "Men" 1 "Women"
label values gender gender_lbl

gen legacy100=legacy*100

** Set colors 
global color1 "248 118 109"


forvalues i = 1/18 {
capture tab gender legacy if ccode==`i', row chi2 // Run a Chi2 for each country
capture local p = string(`r(p)', "%3.2f") // Store the p value in a local variable
capture local t : label cou_lbl `i' // Store the lavbl of the country
capture graph bar (mean) legacy100 if ccode==`i', over(gender, axis(lw(none))) yscale(r(0 50)) /// Create the graph bar
ytitle("") scheme(s1color)  /// Design and colors of the graph
	plotregion(color(white)) yscale(lc(black))  /// 
	bar(1, color(navy*.9) lcolor(none)) title(, box /// 
	bexpand fcolor(gs13) lwidth(none) size(medium)) /// 
	ylabel(0(10)50,grid glcolor(gs1) glp(dot) tlc(black) ) /// 
	subtitle("P-value = `p'", size() box bexpand lwidth(none) fcolor(gs14)) title(`t', pos(12)) name(g`i'_with_p, replace) nodraw
}


graph combine g1_with_p g2_with_p g3_with_p g4_with_p g5_with_p g6_with_p g16_with_p g7_with_p g8_with_p g9_with_p ///
g10_with_p g11_with_p g12_with_p g13_with_p g15_with_p g17_with_p g18_with_p, l1(Percetage of legislators with family ties, size(small)) graphregion(fcolor(white)) ycommon
graph export "figure_a1_color.tif", replace width(1000)







